package com.admin.service.impl;

import com.admin.common.enums.DeclareTypeEnum;
import com.admin.common.exception.BizException;
import com.admin.common.shiro.ShiroUtils;
import com.admin.common.util.DateUtils;
import com.admin.domain.*;
import com.admin.mapper.*;
import com.admin.req.system.SwitchReq;
import com.admin.service.SysParamService;
import com.admin.vo.declare.DeclareTime;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Slf4j
@Service
public class SysParamServiceImpl implements SysParamService {

    @Autowired
    private SysParamMapper sysParamMapper;
    @Autowired
    private DeclareSwitchLogMapper switchLogMapper;
    @Autowired
    private DeclareMapper declareMapper;


    @Override
    public Integer getCurrentDeclareStartTime() {
        DeclareSwitchLog switchLog = switchLogMapper.getLast();
        if (Objects.isNull(switchLog)) {
            throw new BizException(-1, "获取本轮申报时间异常，请确认申报是否开启!");
        }
        return switchLog.getStartTime();
    }

    @Override
    public DeclareTime getDeclareTime() {
        DeclareSwitchLog switchLog = switchLogMapper.getLast();
        if (Objects.isNull(switchLog)) {
            throw new BizException(-1, "获取本轮申报时间异常，请确认申报是否开启!");
        }

        DeclareTime declareTime = new DeclareTime();
        declareTime.setStartTime(switchLog.getStartTime());
        declareTime.setEndTime(switchLog.getEndTime());
        if (null == declareTime.getEndTime() || 0 == declareTime.getEndTime()) {
            declareTime.setEndTime(DateUtils.getTimeStamp());
        }
        return declareTime;
    }

    @Override
    public SysParam declareSwitch() {
        SysParam sysParam = sysParamMapper.getByNameType("sys_param_name", "declare_switch");
        return sysParam;
    }

    @Override
    public DeclareSwitchLog getLastDeclare() {
        return switchLogMapper.getLast();
    }

    @Transactional
    @Override
    public boolean updateSwitch(SwitchReq switchReq) {
        SysParam param = sysParamMapper.selectByPrimaryKey(switchReq.getId());
        if (Objects.isNull(param)) {
            throw new BizException(-1, "参数不存在或已删除!");
        }
        if (param.getValue().equals(switchReq.getValue())) {
            if (param.getValue().equals("true")) {
                throw new BizException(-1, "开关已开启,勿重复操作!");
            } else if (param.getValue().equals("false")){
                throw new BizException(-1, "开关已关闭,勿重复操作!");
            }
        }
        SysParam update = new SysParam();
        update.setId(param.getId());
        update.setValue(switchReq.getValue());
        sysParamMapper.updateByPrimaryKeySelective(update);

        //如果开启， 起来开始时间
        if (switchReq.getValue().equals("true")) {
            DeclareSwitchLog switchLog = new DeclareSwitchLog();
            switchLog.setStartTime(DateUtils.getTimeStamp());
            switchLog.setContent("关闭申报窗口后可出结果");
            switchLog.setOperatorId(ShiroUtils.getUserId());
            switchLog.setOperatorName(ShiroUtils.getRealname());
            switchLog.setOperatorMobile(ShiroUtils.getUser().getMobile());
            switchLogMapper.insertSelective(switchLog);
        } else {
            DeclareSwitchLog switchLog = switchLogMapper.getLast();
            if (!Objects.isNull(switchLog)) {
                DeclareSwitchLog updateLog = new DeclareSwitchLog();
                updateLog.setId(switchLog.getId());
                updateLog.setEndTime(DateUtils.getTimeStamp());
                updateLog.setContent(getContent(updateLog));
                updateLog.setOperatorId(ShiroUtils.getUserId());
                updateLog.setOperatorName(ShiroUtils.getRealname());
                updateLog.setOperatorMobile(ShiroUtils.getUser().getMobile());
                switchLogMapper.updateByPrimaryKeySelective(updateLog);
            }
        }
        return true;
    }

    private String getContent(DeclareSwitchLog switchLog) {
        StringBuffer jsonObject = new StringBuffer();
        Map<String,Object> params = new HashMap<>();
        params.put("startTime", DateUtils.dateFormatStr(switchLog.getStartTime()));
        params.put("endTime", DateUtils.dateFormatStr(switchLog.getEndTime()));
        params.put("declareType", DeclareTypeEnum.NORMAL.getKey());
        jsonObject.append(DeclareTypeEnum.NORMAL.getName()).append(":").append(declareMapper.countByMap(params)).append(",");

        params.put("declareType", DeclareTypeEnum.UPGRADE.getKey());
        jsonObject.append(DeclareTypeEnum.UPGRADE.getName()).append(":").append(declareMapper.countByMap(params));
        return jsonObject.toString();
    }

    @Override
    public List<DeclareSwitchLog> getSwitchLogs() {
        Map<String,Object> map = new HashMap<>();
        List<DeclareSwitchLog> declareSwitchLogList = switchLogMapper.selectByMap(map);
        if (!CollectionUtils.isEmpty(declareSwitchLogList)) {
            for (DeclareSwitchLog switchLog : declareSwitchLogList) {
                switchLog.setStartTimeStr(DateUtils.dateFormatYmdHmStr(switchLog.getStartTime()));
                if (!StringUtils.isEmpty(switchLog.getEndTime())) {
                    switchLog.setEndTimeStr(DateUtils.dateFormatYmdHmStr(switchLog.getEndTime()));
                }
            }
        }
        return declareSwitchLogList;
    }

}
